package org.biodas.jdas.client.threads;

import java.util.concurrent.*;

/**
 * Created by IntelliJ IDEA.
 * User: ljgarcia
 * Date: 28/02/12
 * Time: 17:12
 * To change this template use File | Settings | File Templates.
 */
public class ThreadPoolExecutorJdas extends ThreadPoolExecutor {
    private int threadCounting = 0;
    public ThreadPoolExecutorJdas(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    public ThreadPoolExecutorJdas(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    public ThreadPoolExecutorJdas(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    public ThreadPoolExecutorJdas(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }

    @Override
    public void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t, r);
        this.threadCounting++;
    }

    @Override
    public void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        this.threadCounting--;
    }

    /**
     * Gets the threadCounting value, it keeps the real size of the execution queue.
     * @return
     */
    public int getThreadCounting() {
        return this.threadCounting;
    }

    /**
     * Returns true when any thread is still alive, false otherwise.
     * Note that it could be return 1 when the only thread running has already finished but has not been released.
     * @return
     */
    public boolean hasAliveRunners() {
        return this.threadCounting != 0;
    }
}
